package com.sudichina.sudichina.view.activity.dentification;

import android.content.Intent;
import android.os.Bundle;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.bean.User;
import com.sudichina.sudichina.view.activity.login.LoginActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;

public class IdcardManagerActivity extends BaseActivity implements onRequestDataFinish {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_idcard_manager);
        //检测是否登录
        LoginActivity.startMeAndInitIsLogin(this, Contains.RENZHENG_EXTRA);
        if (MyApp.getCurrentUser() == null) {
            return;
        }
        isConrim();


    }


    private void isConrim() {

        //查询车主的认证信息
        //如果已经认证 显示认证信息  services/carOwner/loadCarOwnerByAccountId
        // /{accountId}
        //判断当前的用户是否登录

        //查询用户的认证信息

        if (MyApp.getCurrentUser() != null) {
            Type type = new TypeToken<Result<User>>() {
            }.getType();

            NetUtils.requestData(Api.LOADCAROWNERBYACCOUNTID + MyApp.getCurrentUser().getId(), null, this, type, false);
        }


    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        if (catalog.equals(Api.LOADCAROWNERBYACCOUNTID + MyApp.getCurrentUser().getId())) {

            Result<User> result = (Result<User>) bean;

            int code = result.getCode();

            if (code == 1) {
                //获取成功
                User data = result.getData();


                /**
                 * "msg": "成功！",
                 "data":
                 {
                 "id": 25,
                 "status": null,
                 "gmtCreate": 1473210778000,
                 "gmtModified": 1473210778000,
                 "realName": "武小鹏",
                 "accountId": 37,
                 "drivingLicense": "585214758525369855",
                 "drivingLicensePath": "http://192.168.1.35:8080/sudichina/upload//2016//09//07//eec56ab9-5d6b-49bf-bbda-6da170db5e70.jpg",
                 "accountProxyId": null,
                 "auditStatus": null,
                 "createDate": null,
                 "recordSource": null,
                 "proxyType": null,
                 "dataStatus": null
                 },
                 "code": 1,
                 "success": true

                 */
                String auditStatus = data.getAuditStatus();


                if (auditStatus == null) {
                    //没有认证
                    //显示认证的界面
                    startActivity(new Intent(IdcardManagerActivity.this, IdcardManagerActivity.class));
                    finish();

                } else if (auditStatus.equals("1")) {
                    //认证过
                    //显示信息的界面
                    Intent intent = new Intent(IdcardManagerActivity.this, IdcardDetailActivity.class);
                    User mUser = result.getData();
                    intent.putExtra("mUser",mUser);
                    startActivity(intent);
                    finish();

                }

            }


        }

    }

    @Override
    public void requestdataFailed(IOException e) {

    }
}
